﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Entities;

namespace CoVanHocTapData.Concretes
{
    public class MonHocSinhVienDangKyKHHTRepository
    {
        DangKyPhuEntities entities = new DangKyPhuEntities();
        HocKyRepository hocKyRepos = new HocKyRepository();

        public List<MonHocSinhVienDangKyKHHT> LayDanhSachSinhVienDangKy(HocKy hocKy,string TenKhoa, string TenNganh)
        {
            //HocKy LayHocKy = hocKyRepos.LayHocKyTheoID(hocKyID).FirstOrDefault();
            var monHocDuKienMoList = (from m in entities.MonDuKienMoes
                                     join n in entities.SinhVienMonDuKienMoes
                                     on m.MonDuKienMoID equals n.MonDuKienMoID
                                     where m.HocKyID == hocKy.HocKyID
                                    && n.SinhVien.Lop.NganhDaoTao.TenNganh.Equals(TenNganh)
                                 select m).Distinct();
            int tempInt = monHocDuKienMoList.Count();
            List<MonHocSinhVienDangKyKHHT> result = new List<MonHocSinhVienDangKyKHHT>();
            foreach (var item in monHocDuKienMoList)
            {
                var sinhVienDangKyList = from m in entities.SinhVienMonDuKienMoes
                                         where m.MonDuKienMoID == item.MonDuKienMoID                                         
                                         select m;

                int dem = sinhVienDangKyList.Count();
                if (dem != 0)
                {
                    MonHocSinhVienDangKyKHHT dangKyItem = new MonHocSinhVienDangKyKHHT()
                    {
                        HocKy = hocKy.HocKy1.Value,
                        MonHocID = item.MonHocID,
                        NamHoc = hocKy.NamHoc.Value,
                        SoLuongDK = dem,
                        TenHocKy = hocKy.TenHocKy,
                        TenKhoa = TenKhoa,
                        TenNganh = TenNganh,
                        TenMonHoc = item.MonHoc.TenMonHoc,

                    };
                    result.Add(dangKyItem);
                }
            }
            return result;
        }
    }
}
